package com.yanti.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanti.domain.ResponseResult;
import com.yanti.domain.entity.QuestionChapters;
import com.yanti.domain.vo.ChaptersVo;
import com.yanti.mapper.QuestionChaptersMapper;
import com.yanti.service.QuestionChaptersService;
import com.yanti.service.QuestionsService;
import com.yanti.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (QuestionChapters)表服务实现类
 *
 * @author luo
 * @since 2024-12-04 09:13:14
 */
@Service("questionChaptersService")
public class QuestionChaptersServiceImpl extends ServiceImpl<QuestionChaptersMapper, QuestionChapters> implements QuestionChaptersService {

    @Autowired
    private QuestionsService questionsService;

    @Override
    public ResponseResult getAllChapter() {
        List<QuestionChapters> chapters = list();
        List<ChaptersVo> chaptersVos = BeanCopyUtils.copyBeanList(chapters, ChaptersVo.class);
        List<ChaptersVo> chaptersVoList = chaptersVos.stream()
                .map(chaptersVo -> chaptersVo.setQuestionNum(questionsService.getQuestionNumByChapterId(chaptersVo.getId())))
                .toList();
        return ResponseResult.okResult(chaptersVoList);
    }

    @Override
    public ResponseResult getChapterById(String id) {
        QuestionChapters chapter = getById(id);
        return ResponseResult.okResult(chapter);
    }

    @Override
    public ResponseResult getChaptersBySubjectId(String subjectId) {
        LambdaQueryWrapper<QuestionChapters> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(QuestionChapters::getSubjectId,subjectId);
        List<QuestionChapters> chapters = list(queryWrapper);
        if(chapters != null){
            List<ChaptersVo> chaptersVos = BeanCopyUtils.copyBeanList(chapters, ChaptersVo.class);
            List<ChaptersVo> chaptersVoList = chaptersVos.stream()
                    .map(chaptersVo -> chaptersVo.setQuestionNum(questionsService.getQuestionNumByChapterId(chaptersVo.getId())))
                    .toList();
            return ResponseResult.okResult(chaptersVoList);
        }
        return null;
    }
}